<template>
  <div class="container">
    <el-form ref="addTags" class="form" :model="form" :rules="rules">
      <el-form-item label="所属学科" prop="subjectID">
        <el-select v-model="form.subjectID" ref="forsm" placeholder="请选择">
          <el-option
            v-for="item in subjects"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" prop="tagName">
        <el-input
          class="inputx"
          v-model="form.tagName"
          placeholder="请输入目录名称"
        ></el-input>
      </el-form-item>
      <div class="button">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="comfirm">确 定</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/tags.js'
import { simple } from '@/api/hmmm/subjects.js'
export default {
  name: 'Tagsadd',
  props: ['tagDate', 'isEdit', 'tagId'],

  data () {
    return {
      dialogVisible: false, // 弹框展示隐藏
      form: {
        subjectID: '',
        tagName: ''
      },
      rules: {
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ],
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: 'blur' }
        ]
      },
      subjects: []

    }
  },
  created () {
    if (this.isEdit === true) {
      this.form = this.tagDate
    }
    this.simple()
  },
  methods: {
    // 清空表单数据
    restForm () {
      this.form = {
        subjectID: '',
        tagName: ''
      }
      this.$refs.addTags.resetFields()
    },

    // 获取标签名称
    async simple () {
      const res = await simple(this.tagName)
      // console.log(res.data)s
      this.subjects = res.data
    },

    // 关闭弹框
    cancel () {
      this.$emit('close')
    },

    // 添加标签/编辑修改
    comfirm () {
      this.$refs.addTags.validate(async valid => {
        if (!valid) return false
        if (this.isEdit) {
          // console.log(1)
          const res = await update({ ...this.form, id: this.tagId })
          console.log(res)
        } else {
          await add(this.form)
        }
        this.$message.success('操作成功')
        this.$emit('changeData')
        this.cancel()
      })
    }

  }
}
</script>

<style scoped >
.button {
  padding-left: 195px;
  padding-top: 23px;
}
.form {
  width: 400px;
  height: 160px;
}
.inputx {
  width: 50%;
}
</style>
